package com.bcu.cn.onlineexamsystem.studentmanage.controller;

import com.bcu.cn.onlineexamsystem.studentmanage.entity.Question;
import com.bcu.cn.onlineexamsystem.studentmanage.entity.StudentAnswer;
import com.bcu.cn.onlineexamsystem.studentmanage.entity.StudentExam;
import com.bcu.cn.onlineexamsystem.user.entity.User;
import com.bcu.cn.onlineexamsystem.studentmanage.service.StudentExamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/student/exam")
public class StudentExamController {

    @Autowired
    private StudentExamService examService;

    @GetMapping("/start/{examId}")
    public String startExam(@PathVariable Long examId, HttpSession session, Model model) {
        User user = (User) session.getAttribute("user");
        String studentId = user.getUserId();
        
        // 开始考试
        StudentExam studentExam = examService.startExam(studentId, examId);
        System.out.println("考试开始信息:"+studentExam);
        // 获取考试题目
        List<Question> questions = examService.getExamQuestions(examId);
        
        // 获取考试时长
        Integer duration = examService.getExamDuration(examId);
        
        model.addAttribute("studentExam", studentExam);
        model.addAttribute("questions", questions);
        model.addAttribute("duration", duration);
        
        return "student/exam/take";
    }

    @PostMapping("/save")
    @ResponseBody
    public String saveAnswers(@RequestBody List<StudentAnswer> answers) {
        for (StudentAnswer answer : answers) {
            examService.saveAnswer(answer);
        }
        return "success";
    }

    @PostMapping("/submit")
    @ResponseBody
    public String submitExam(@RequestBody List<StudentAnswer> answers) {
        if (answers == null || answers.isEmpty()) {
            return "no answers";
        }
        Long recordId = answers.get(0).getRecordId();
        examService.submitExam(recordId, answers);
        return "success";
    }

    @GetMapping("/status/{examId}")
    @ResponseBody
    public StudentExam getExamStatus(@PathVariable Long examId, HttpSession session) {
        String studentId = (String) session.getAttribute("userId");
        return examService.getExamStatus(studentId, examId);
    }
    
    @GetMapping("/duration/{examId}")
    @ResponseBody
    public Integer getExamDuration(@PathVariable Long examId) {
        return examService.getExamDuration(examId);
    }

    @GetMapping("/answers/{recordId}")
    @ResponseBody
    public List<StudentAnswer> getSavedAnswers(@PathVariable Long recordId) {
        return examService.getSavedAnswers(recordId);
    }

    @GetMapping("/info/{examId}")
    @ResponseBody
    public ExamInfoDTO getExamInfo(@PathVariable Long examId, HttpSession session) {
        User user = (User) session.getAttribute("user");
        String studentId = user.getUserId();
        StudentExam studentExam = examService.getExamStatus(studentId, examId);
        Integer duration = examService.getExamDuration(examId);
        ExamInfoDTO dto = new ExamInfoDTO();
        dto.setRecordId(studentExam.getRecordId());
        dto.setStartTime(studentExam.getStartTime());
        dto.setDuration(duration);
        return dto;
    }

    @GetMapping("/detail/{examId}")
    public String examDetail(@PathVariable Long examId, HttpSession session, Model model) {
        // 获取当前学生ID
        User user = (User) session.getAttribute("user");
        String studentId = user.getUserId();
        // 查询该学生本场考试的recordId
        Long recordId = examService.getStudentExamRecordId(studentId, examId);
        // 查询考试详情（含题目、作答、得分）
        Map<String, Object> examDetail = examService.getExamDetailForStudentWithAnswer(examId, recordId);
        // 确保recordId放入Map，供前端PDF导出使用
        examDetail.put("recordId", recordId);
        model.addAttribute("examDetail", examDetail);
        return "student/exam/detail";
    }
} 